<?php
class c_home extends CI_Controller{
    
    private $data;
    
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('m_model');
    }
    
    public function index(){
        $this->data["page_title"] = "Trang chủ";
        $this->data["page_left"] = "v_home";
        $this->data["page_right"] = "v_right_side_home";

        $this->renderContent();
        $this->renderRightBar();
        $this->load->view("v_masterpage.php", $this->data);
    }
    
    public function send_mail(){
        $address = $_POST['address'];
        $subject = $_POST['subject'];
        $cookie_name = "system_mail";
        $cookie_value = "1";
        if($address != null && $subject != null && !isset($_COOKIE[$cookie_name])){
            setcookie($cookie_name, $cookie_value);
            $result = $this->m_model->insert_email($address, $subject);
            echo $result;
        }
        echo 0;
    }
    
    public function tag_page(){
        $this->data["page_title"] = "Trang chủ";
        $this->data["page_left"] = "v_tag_page";
        $this->data["page_right"] = "v_right_side_home";
        $this->data["list_tag"] = $this->m_model->get_tag_list(0);

        $this->renderRightBar();
        $this->load->view("v_masterpage.php", $this->data);
    }

    private function renderContent(){
        $pageIndex = isset($_GET["page"]) ? $_GET["page"] : 1;
        $pageSize = 10;
        $tag = "";
        if(isset($_GET['tag']) && $_GET['tag'] != ""){
            $tag = $_GET['tag'];
            list($count, $listNews) = $this->m_model->get_news_by_tag(($pageIndex - 1) * $pageSize, $pageSize, $tag);
        }
        else{
            list($count, $listNews) = $this->m_model->get_list_news(($pageIndex - 1) * $pageSize, $pageSize, 1);
        }
        $this->data["listNews"] = $listNews;
        $this->data["pageIndex"] = $pageIndex;
        $numberOfPage = ceil($count / $pageSize);
        $pathInfo = $tag == "" ? "/?" : "/?tag=" . $tag . "&";
        $this->paging($pathInfo, $numberOfPage, $pageIndex);
    }

    private function renderRightBar(){
        $bestNews = $this->m_model->getBestNewsRead();
        $this->data["listBestNews"] = $bestNews;
        
        $bestVideo = $this->m_model->getBestViewVideo();
        $this->data["listBestVideo"] = $bestVideo;
        
        $tagList = $this->m_model->get_tag_list(10);
        $this->data["tagList"] = $tagList;
    }

    private function paging($pathInfo, $numberOfPage, $currentPage){
        $prevPage = $currentPage == 1 ? 1 : $currentPage - 1;
        $nextPage = $currentPage == $numberOfPage ? $numberOfPage : $currentPage + 1;
        $lastPage = $numberOfPage;

        $start_page = 1;
        $end_page = $numberOfPage;
        if($numberOfPage > 5){
            $start_page = $currentPage - 2 < 1 ? 1 : $currentPage - 2;
            $end_page = $currentPage + 2 > $numberOfPage ? $numberOfPage : $currentPage + 2;
        }

        $html = "<span class='other_page'><a href='" . $pathInfo . "page=1'>Đầu</a></span>"
            . "<span class='other_page'><a href='" . $pathInfo . "page=" . $prevPage . "'>Trước</a></span>";
        for($i=$start_page; $i<=$end_page; $i++){
            if($i == $currentPage){
                $html .= "<span class='current_page'>" . $i . "</span>";
            }
            else{
                $html .= "<span class='other_page'><a href='" . $pathInfo . "page=" . $i . "'>" . $i . "</a></span>";
            }
        }
        $html .= "<span class='other_page'><a href='" . $pathInfo . "page=" . $nextPage . "'>Sau</a></span>"
            . "<span class='other_page'><a href='" . $pathInfo . "page=" . $lastPage . "'>Cuối</a></span>";

        $this->data["paging"] = $html;
    }
    
}